<?php
namespace app\admin\model;
use app\common\model\CommonModel;
use app\common\model\Rule;
use think\Session;

class RuleModel extends CommonModel
{
	/**
	* 添加规则信息
	*/
	public static function serRule($info)
	{
		if(array_key_exists('ty',$info))
		{
			$data['ty'] = $info['ty'];
		}
		$data['name'] = $info['name'];
		$data['param'] = $info['param'];
		$data['title'] = $info['title'];
		$data['pid'] = $info['pid'];
		$data['level'] = $info['level'];
		$data['sort'] = $info['sort'];
		$data['ascription'] = $info['ascription'];
		$data['nav'] = $info['nav'];
		if(!(new Rule())->serRule($data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 修改规则信息
	*/
	public static function updateRule($info)
	{
		if(array_key_exists('ty',$info))
		{
			$data['ty'] = $info['ty'];
		}
		$data['name'] = $info['name'];
		$data['param'] = $info['param'];
		$data['title'] = $info['title'];
		$data['pid'] = $info['pid'];
		$data['level'] = $info['level'];
		$data['sort'] = $info['sort'];
		$data['ascription'] = $info['ascription'];
		$data['nav'] = $info['nav'];
		if(!(new Rule())->updateRule(['arid'=>$info['id']],$data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	*获取父级信息
	*/ 
	public static function ruleList()
	{
		$Rule = new Rule();
		$data = $Rule->ruleList(['pid'=>CommonModel::NUMBER_ZERO],'arid,title,ascription');
		foreach($data as $k=>$vo)
		{
			$data[$k]['xiaji'] = $Rule->ruleList(['pid'=>$vo['arid']],'arid,title,ascription');
		}
		return $data;
	}
	/**
	*获取规则列表
	*/ 
	public static function getRuleList($limit,$page)
	{
		$Rule = new Rule();
		$ruleList = $Rule->getRuleList($limit,$page);
		/* if(!$ruleList['data'])
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		} */
		foreach($ruleList['data'] as $k=>$v)
		{
			if($v['pid'] == CommonModel::NUMBER_ZERO)
			{
				$ruleList['data'][$k]['pid'] = '顶级';
			}else{
				$ruleList['data'][$k]['pid'] = $Rule->getRuleField(['arid'=>$v['pid']],'title');
			}
			$ruleList['data'][$k]['ascription'] = self::ascription($v['ascription']);
			$ruleList['data'][$k]['nav'] = $v['nav'] == CommonModel::NUMBER_ZERO?'不显示':'显示';
		}
		return $ruleList;
	}
	/**
	* 删除
	*/
	public static function delRule($id)
	{
		if((new Rule())->delRule(['arid'=>$id]) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 归属整理
	*/
	private function ascription($ascription)
	{
		switch($ascription)
		{
			case 0:return '总部';break;
			case 1:return '营业员';break;
			case 2:return '维修员';break;
		}
	}
}
